import {userUpdate, query,queryVerificationCode,initialization,checkLogin,requserCasLogin } from '@/services/user';
import {queryUserInfo,getQueryVariable }from '../utils/utils'
import { setAuthority ,getAuthority } from '@/utils/authority';
import { reloadAuthorized } from '@/utils/Authorized';
/*function initPlatformInfo (){

  const profile = getQueryVariable("profile");
  const authCode = getQueryVariable("code");
  const authState = getQueryVariable("state");
  if(profile){//如果?后面已经有profile了的情况下
    const basic_info = checkProfile(profile);
    if(basic_info.appModal && basic_info.platformName){
      window.sessionStorage.setItem("APP_MODAL", basic_info.appModal);
      window.sessionStorage.setItem("PLATFORM_NAME", basic_info.platformName);
    }
  }else if (authCode&&authState){
    const basic_info = checkProfile(authState);
    if(basic_info.appModal && basic_info.platformName){
      window.sessionStorage.setItem("APP_MODAL", basic_info.appModal);
      window.sessionStorage.setItem("PLATFORM_NAME", basic_info.platformName);
    }
    return {
      oauth_code : authCode
    }
  }
  return {}
}*/
export default {
  namespace: 'user',

  state: {
    list: [],
    currentUser: {},
    login_state : 'loading',
  },

  effects: {
    *fetch(_, { call, put }) {
      console.log("come here");
      const user_roles = getAuthority();
      if(!user_roles || user_roles.length ==0  || user_roles == 'notLogin'){
        window.location.href = '/user/login'
      }else {
        const user_info = queryUserInfo();
        yield put({
          type: 'saveCurrentUser',
          payload: {
            loading:false,
            name :user_info.user_nick_name ,
            is_init_pwd:1
          },
        });
      }

      //const loginResponse =  yield call(checkLogin,{});
      /*if(loginResponse && loginResponse.return_code === 'SUCCESS'){
        let userInfo;
        const user_roles = getAuthority();
        if(!user_roles || user_roles.length ==0  || user_roles == 'notLogin'){
          const response =  yield call(initialization,{});
          if (response.return_code === 'SUCCESS') {
            window.sessionStorage.setItem("userPermissions", JSON.stringify(response.data.permission_roles?response.data.permission_roles:[]));
            window.sessionStorage.setItem("topUrls", JSON.stringify(response.data.permission_bigScreen?response.data.permission_bigScreen:[]));
            setAuthority(response.data.user_roles);
            userInfo = {
              ... response.data.user_info ? response.data.user_info : undefined,
              project_id: response.data.project_id,
              is_init_pwd:response.data.is_init_pwd?response.data.is_init_pwd:0
            }
            window.sessionStorage.setItem("userInfo", JSON.stringify(userInfo));
            reloadAuthorized();
            window.location.href = window.location.pathname + window.location.search;  //在此处判断每个用户登陆后进入哪个页面
          }
        }else{
          userInfo = queryUserInfo()
          yield put({
            type: 'saveCurrentUser',
            payload: {
              loading:false,
              name :userInfo?userInfo.user_name:'',
              is_init_pwd:userInfo.is_init_pwd?userInfo.is_init_pwd:0
            },
          });
        }
      }else if(loginResponse && loginResponse.return_code === 'NOTLOGIN'){

          const response =  yield call(requserCasLogin);
          if(response.return_code==='SUCCESS'){
            let redirectUrl = response.return_msg;
            window.location.href = redirectUrl;
          }
      }else{
        message.error(loginResponse.return_msg);
      }*/
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    saveCurrentUser(state, action) {
      return {
        ...state,
        login_state : action.payload.loading,
        currentUser: {name:action.payload.name} || {},
        is_init_pwd:action.payload.is_init_pwd
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
        },
      };
    },
  },
};
